﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    /// <summary>
    /// 表示一个特性，该特性用于限制操作方法，以便该方法仅处理 HTTP AJAX 请求。
    /// </summary>
    public class HttpAjaxAttribute : ActionMethodSelectorAttribute
    {

        protected virtual string HttpMethod
        { get { return string.Empty; } }

        public override bool IsValidForRequest(ControllerContext controllerContext, Reflection.MethodInfo methodInfo)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (controllerContext.HttpContext == null)
            {
                return false;
            }
            HttpRequestBase request = controllerContext.HttpContext.Request;
            if (request == null)
            {
                return false;
            }
            if (!string.IsNullOrEmpty(HttpMethod) && !request.GetHttpMethodOverride().Equals(HttpMethod, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            return request.IsAjaxRequest();
        }
    }
}

 


